When you make your own website using third-party clouds, e.g. Heroku, there is a restriction for the uploaded data size. For example, Heroku only allows 500MB memory space. This might become a too tight constraint if you want to add some pictures to your websites as high resolution pictures nowadays could easily be about 10MB.
Instead of uploading photos to these clouds together with your .html and .css files, it may be wise to use other image hosting service such as Flickr or Instagram: you may upload images to these image hosting services, make them public, and then simply add link to the photos in the image hosting service in your webpage.
In this blog post, I will explore this approach and present how to extract pictures from Flickr's public album using python.
I assume that you know the user_id of the owner of Flickr's public album. user_id is most likely of the form: 123456789@N12. For example my user id is 157237655@N08
See here to find user_id of Fliker users.
References¶
Step 1: Get photoset id via flickr.photosets.getList endpoint¶
To extract photos in Flikr's public albums, I need to know a photoset id. This is an identifier for the album.
import requests
import json, sys
sys.path.append('../')
from personal import flikr_api_key as api_key
def get_requestURL(user_id,endpoint="getList"):
user_id = user_id.replace("@","%40")
url_upto_apikey = ("https://api.flickr.com/services/rest/?method=flickr.photosets." +
endpoint +
"&api;_key=" + api_key +
"&user;_id=" + user_id +
"&format;=json&nojsoncallback;=1")
return(url_upto_apikey)
user_id = "157237655@N08"
url = get_requestURL(user_id,endpoint="getList")
strlist = requests.get(url).content
json_data = json.loads(strlist)
albums = json_data["photosets"]["photoset"]
print("{} albums found for user_id={}".format(len(albums),user_id))
Let's look at some of the album titles
photosetids, titles = [], []
for album in albums:
print("___")
print("album title={} photoset_id={}".format(album['title']['_content'],album["id"]))
photosetids.append(album["id"])
titles.append(album['title']['_content'])
Step 2: For each album, extract infomation of all the photos.¶
In order to find the unique URL to each photo, I need to know:
- farm ID
- server ID
- ID
- secret
Such infomation is extracted using flickr.photosets.getPhotos API.
def get_photo_url(farmId,serverId,Id,secret):
return (("https://farm" + str(farmId) +
".staticflickr.com/" + serverId +
"/" + Id + '_' + secret + ".jpg"))
URLs = {}
for photoset_id, title in zip(photosetids,titles): ## for each album
url = get_requestURL(user_id,endpoint="getPhotos") + "&photoset;_id=" + photoset_id
strlist = requests.get(url).content
json1_data = json.loads(strlist)
urls = []
for pic in json1_data["photoset"]["photo"]: ## for each picture in an album
urls.append(get_photo_url(pic["farm"],pic['server'], pic["id"], pic["secret"]))
URLs[photoset_id] = urls
Finally let's plot the extracted photos from the first 4 albums¶
The codes seem to be working!
from IPython.display import Image, display
count = 1
for i, (photoset_id, urls) in enumerate(URLs.items()):
print("______________________")
print("{}, photoset_id={}".format(titles[i],photoset_id))
for url in urls:
print(url)
display(Image(url= url, width=200, height=200))
count += 1
if count > 4:
break